package com.nit.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nit.server.common.QueryPageParam;
import com.nit.server.common.RentApplyParam;
import com.nit.server.common.Result;
import com.nit.server.common.UseApplyParam;
import com.nit.server.entity.Rentapply;
import com.nit.server.entity.Rentgoods;
import com.nit.server.entity.Useapply;
import com.nit.server.service.RentapplyService;
import com.nit.server.service.RentgoodsService;
import com.nit.server.service.UseapplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * 租用物品管理
 *
 */
@RestController
@RequestMapping("/rentgoods")
public class RentgoodsController {
    @Autowired
    private RentgoodsService rentgoodsService;
    @Autowired
    private RentapplyService rentapplyService;

    //新增
    @PostMapping("/add")
    public Result add(@RequestBody Rentgoods rentgoods){
        return rentgoodsService.save(rentgoods)?Result.success():Result.fail();
    }
    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Rentgoods rentgoods){
        return rentgoodsService.updateById(rentgoods)?Result.success():Result.fail();
    }
    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String rentgoodsId){
        return rentgoodsService.removeById(rentgoodsId)?Result.success():Result.fail();
    }
    @PostMapping("/listPage")
//    public List<rentr> listPage(@RequestBody HashMap map){
    public Result listPage(@RequestBody QueryPageParam pageParam){
        System.out.println(pageParam);
        String name = (String)pageParam.getParams().get("rentgoodsName");
        String cId = (String)pageParam.getParams().get("cId");
        Page<Rentgoods> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());
        QueryWrapper<Rentgoods> queryWrapper = new QueryWrapper();
        queryWrapper.apply("a.c_id = b.c_id ");
        if (StringUtils.isNotBlank(cId)){
            queryWrapper.eq("a.c_id",cId);
        }
        if (StringUtils.isNotBlank(name)){
            queryWrapper.like("a.rentgoods_name",name);
        }
//        IPage result = rentrService.pageC(page);
        IPage result = rentgoodsService.pageCC(page,queryWrapper);

        return Result.success(result.getTotal(), result.getRecords());
    }
    @GetMapping("/list")
    public Result list(){
        List list = rentgoodsService.list();

        return Result.success(list);
    }
    //加仓
    @PostMapping("/addsave")
    public Result addsave(@RequestBody Rentgoods rentgoods){
//        System.out.println(record);
        Rentgoods rentgoods1 = rentgoodsService.getById(rentgoods.getRentgoodsId());
        int n = rentgoods.getRentgoodsCount();
        int newnum = n+rentgoods1.getRentgoodsCount();
        rentgoods1.setRentgoodsCount(newnum);
        return rentgoodsService.updateById(rentgoods1)?Result.success():Result.fail();
    }
    //租用
    @PostMapping("/rentsave")
    public Result rentsave(@RequestBody RentApplyParam param){
        System.out.println(param);
        int rentgoodsId = param.getRentgoodsId();
        String rentgoodsCount = param.getRentgoodsCount();
        String rentinfo = param.getRentinfo();
        int userID = param.getUserId();

        Rentapply rentapply = new Rentapply();
        rentapply.setRentgoodsId(rentgoodsId);
        rentapply.setRentapplyCount(Integer.valueOf(rentgoodsCount));
        rentapply.setRentapplyInfo(rentinfo);
        rentapply.setUserId(userID);

        return rentapplyService.save(rentapply)?Result.success():Result.fail();
    }
}
